/**
 *  @author phi
 *  @memo   
 */
#pragma once

#include "tmlib.h"
#include "game-app-manager.h"

class AnimatorTest : public Application
{
public :
    void initialize()
    {
        tmlib::display::DisplayObjectContainer* obj = new tmlib::display::DisplayObjectContainer();
        _root.addChild(obj);

        obj->x(95).y(95).width(10).height(10);
        obj->addEventListener(events::EventInfo::MOUSE_DOWN, onMouseOver);
        obj->addEventListener(events::EventInfo::MOUSE_UP, onMouseOut);
        _animator.motion()->setValue(1, tmlib::motion::AnimationInfo::X, 0)->setValue(1, tmlib::motion::AnimationInfo::Y, 0);
        _animator.target(obj);
        _animator.play();
        _animator.addEventListener(events::EventInfo::MOTION_END, onMotionEnd);

        {
            tmlib::display::DisplayObject* child = NULL;
        
            child = new tmlib::display::InteractiveObject();
            obj->addChild(child);
            child->x(10).y(10).width(10).height(10);
            child->addEventListener(events::EventInfo::MOUSE_DOWN, onMouseOver);
            child->addEventListener(events::EventInfo::MOUSE_UP, onMouseOut);

            child = new tmlib::display::DisplayObject();
            obj->addChild(child);
            child->x(-10).y(10).width(10).height(10);
            child->addEventListener(events::EventInfo::MOUSE_DOWN, onMouseOver);
            child->addEventListener(events::EventInfo::MOUSE_UP, onMouseOut);

            child = new tmlib::display::DisplayObject();
            obj->addChild(child);
            child->x(-10).y(-10).width(10).height(10);
            child->addEventListener(events::EventInfo::MOUSE_DOWN, onMouseOver);
            child->addEventListener(events::EventInfo::MOUSE_UP, onMouseOut);

            child = new tmlib::display::DisplayObject();
            obj->addChild(child);
            child->x(10).y(-10).width(10).height(10);
            child->addEventListener(events::EventInfo::MOUSE_DOWN, onMouseOver);
            child->addEventListener(events::EventInfo::MOUSE_UP, onMouseOut);
        }

    }

    void finalize()
    {
    }

    void update()
    {
        _root.update();
        _animator.update();
    }
    void draw()
    {
        _root.draw();
    }

    static bool onMouseOut(events::Event* e)
    {
        tmlib::display::InteractiveObject* obj = (tmlib::display::InteractiveObject*)(e->getTarget());
#if 0
        obj->setX(45)->setY(45)->setWidth(10)->setHeight(10);
#else
        obj->setColor(1.0f, 1.0f, 1.0f, 1.0f);
#endif

        return true;
    }

    static bool onMouseOver(events::Event* e)
    {
        static s32 s_hit_num = 0;
        ++s_hit_num;
        cout << "Hit:" << s_hit_num << endl;

        tmlib::display::InteractiveObject* obj = (tmlib::display::InteractiveObject*)(e->getTarget());

    #if 0
        obj->setX(obj->getX()+1);
    #else
        obj->setColor(1.0f, 0.0f, 0.0f, 1.0f);
    #endif

        return true;
    }

    static bool onMotionEnd(events::Event* e)
    {
        tmlib::motion::Animator* obj = (motion::Animator*)(e->getTarget());

        static float s_num = 0;
        s_num+=10;
        f32 x = -cos(s_num*3.14/180.0f)*100 + 95;
        f32 y =  sin(s_num*3.14/180.0f)*100 + 95;
        obj->motion()->setValue(1, tmlib::motion::AnimationInfo::X, x)->setValue(1, tmlib::motion::AnimationInfo::Y, y);
        obj->play();

        return true;
    }


private :
    tmlib::motion::Animator                 _animator;
};